package com.have.blog.web.service.impl;

import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.mapper.WebNavigationBarMapper;
import com.have.blog.model.models.WebNavigationBar;
import com.have.blog.model.models.WebNavigationBarExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.web.dto.WebNavigationBarDto;
import com.have.blog.web.service.WebNavbarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@SuppressWarnings("all")
public class WebNavbarServiceImpl implements WebNavbarService {
    @Autowired
    private WebNavigationBarMapper webNavbarMapper;

    public List<WebNavigationBarDto> getAllList(){
        WebNavigationBarExample example = new WebNavigationBarExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andNavbarLevelEqualTo(1);
        example.setOrderByClause("sort desc");
        List<WebNavigationBar> webNavbarList = webNavbarMapper.selectByExample(example);
        List<WebNavigationBarDto> resultList = MyBeanUtils.copyListProperties(webNavbarList, WebNavigationBarDto::new);
        List<Integer> ids = new ArrayList<>();
        for (WebNavigationBarDto item : resultList) {
            ids.add(item.getUid());
        }
        example.clear();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andParentUidIn(ids);
        List<WebNavigationBar> childList = webNavbarMapper.selectByExample(example);
        List<WebNavigationBarDto> childDtoList = MyBeanUtils.copyListProperties(childList, WebNavigationBarDto::new);
        Map<Integer, List<WebNavigationBarDto>> map = new HashMap<>();
        for (WebNavigationBarDto item : childDtoList) {
            List<WebNavigationBarDto> tempList = map.get(item.getParentUid());
            if (tempList == null) {
                tempList = new ArrayList<>();
                map.put(item.getParentUid(), tempList);
            }
            tempList.add(item);
        }
        for (WebNavigationBarDto item : resultList) {
            item.setChildWebNavbar(map.get(item.getUid()));
        }
        // TODO REDIS
        return resultList;
    }
}
